
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.control;

import edu.hpvas.persistence.dao.OwnerDAO;
import edu.hpvas.persistence.dao.PersonDAO;
import edu.hpvas.persistence.entity.Owner;
import edu.hpvas.persistence.entity.Person;
import java.awt.font.NumericShaper;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sony
 */
public class OwnerController {
    
    private Owner owner = new Owner();
    private OwnerDAO ownerDao = new OwnerDAO();
    private PersonDAO personDao = new PersonDAO();

    public Owner getOwner() {
        return owner;
    }

    public void setOwner(Owner owner) {
        this.owner = owner;
    }

    public OwnerDAO getOwnerDao() {
        return ownerDao;
    }

    public void setOwnerDao(OwnerDAO ownerDao) {
        this.ownerDao = ownerDao;
    }

    public PersonDAO getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDAO personDao) {
        this.personDao = personDao;
    }
    
    public boolean createOwner(Person p){
        
        List<Person> persons = personDao.findPersonEntities();
        int id=1;
        for(Person person: persons){
            if(person.getId().intValue()!=id){
                break;
            }
            id++;
        }
        p.setId(id);
        try {
            personDao.create(p);
        } catch (Exception ex) {
            
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        System.out.println("se creo la persona");
        List<Owner> owneres = ownerDao.findOwnerEntities();
        int idOwner=1;
        for(Owner own: owneres){
            if(own.getId().intValue()!=idOwner){
                break;
            }
            idOwner++;
        }
        owner.setId(idOwner);
        owner.setIdPerson(p);
        try {
            ownerDao.create(owner);
        } catch (Exception ex) {
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
    public ArrayList<Owner> searchOwners(String name){
        
        List<Owner> owners =ownerDao.findOwnerEntities();
        ArrayList<Owner> ownersAux = new ArrayList<>();
        for(Owner o: owners){
            if((o.getIdPerson().getName() + " " + o.getIdPerson().getLastName())
                    .toLowerCase().contains(name.toLowerCase())){
                ownersAux.add(o);
            }
            //p= personDao.findPerson(o.getIdPerson().getId());
        }
        return ownersAux;
    }
    public void deleteOwner(Integer id,Integer idPerson){
        ownerDao.destroy(id);
        personDao.destroy(idPerson);
    }
    public boolean updateowner(Owner owner,Integer idPerson){
        try {
            personDao.edit(owner.getIdPerson());
            ownerDao.edit(owner);
            return true;
        } catch (Exception ex) {
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        
    }
    
}
